package com.study.lib_tiandi_map.tiandimap

import android.content.Context
import android.location.Location
import android.location.LocationManager
import java.lang.Exception

/**
 * 获取当前位置
 */
class GetLocation(private val context: Context) {
    fun location(): Location? {
        val locationManager: LocationManager =
            context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        var location: Location? = null
        try {
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
            if (location == null) {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
            }
            if (location == null) {
                location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return location
    }
}